<!--
* description: 门店销售计划调整比例设置
* author: duanhy
* createdDate: 2020-11-18
-->
<template>
  <div class="app-container app-container-table">
    <!-- 单表格模版页面 -->
    <one-table-template
      ref="multipleTable"
      :dynamic-api-config="apiConfig"
      :dynamic-buttons="dynamicButtons"
      :dynamic-components="tableComponents"
      :dynamic-table-cols="dynamicTableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-more-btn="false"
      :dynamic-is-show-select="true"
      :callback-obj="callbackObj"
    />
    <Edit
      v-if="dialogShow"
      :is-dialog-visible="dialogShow"
      :data-for-form="editRowData"
      :title-name="titleName"
      @close="close"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import Edit from './edit'
import Vue from 'vue'
import api from '@/api/apiVeData'
import commData from '@/views/ve/planReport/common/index.js'
export default {
  name: 'MonthlySalesSetting',
  components: {
    OneTableTemplate,
    Edit
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData(
      // 同时加载当前页面和编辑框页面的配置
      [to.path, ''],
      function() {
        next()
      }
    )
  },
  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      lock: true,
      select: '',
      editRowData: '',
      dialogShow: false,
      // table表格查询API配置对象
      apiConfig: api.queryStorePlanScaleSet,
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents
          : [
            ...commData.ModelOptions(2),
            {
              compKey: 'compKey1',
              labelName: '制定月份：',
              codeField: 'makingMonth',
              component: () => import('@/components/org/datePicker/datePicker'),
              dateType: 'month',
              dateOptionsType: '3', // 禁用当前日期之前的日期
              isMust: false,
              span: 6
            },
            {
              compKey: 'compKey5',
              labelName: '计划月份：',
              codeField: 'planMonth',
              component: () => import('@/components/org/datePicker/datePicker'),
              dateType: 'month',
              dateOptionsType: '3', // 禁用当前日期之前的日期
              isMust: false,
              span: 6
            },
            {
              compKey: 'compKey8',
              labelName: '门店名称：',
              codeField: 'dlrShortName',
              component: () => import('./setting/index'),
              type: 'inputText',
              isMust: false,
              span: 6
            }
          ],
      // form 的表单对象
      formField:
      {
        makingMonth: '',
        planMonth: '',
        dlrShortName: '',
        carSeriesId: '',
        carConfigId: '',
        carColorId: '',
        carIncolorId: '',
        optionalPagId: ''
      },

      // 动态组件-按钮
      dynamicButtons:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableButtons
          : [
            {
              compKey: 'btnKey1',
              type: 'primary',
              size: 'small',
              name: 'search',
              clickEvent: () => this.queryTable(1),
              text: '查询'
            },
            {
              compKey: 'btnKey2',
              type: '',
              size: 'small',
              name: 'reset',
              clickEvent: () => this.reset(),
              text: '重置'
            },
            {
              compKey: 'btnKey3',
              type: '',
              size: 'small',
              name: '',
              position: 'left',
              parentFileds: 'disabled:table',
              clickEvent: () => this.formulate(),
              disabled: true,
              text: '比例设置'
            },
            {
              compKey: 'btnKey4',
              type: '',
              size: 'small',
              name: '',
              position: 'left',
              parentFileds: 'disabled:table',
              disabled: true,
              clickEvent: () => this.synchro(),
              text: '同步'
            }
          ],
      // 动态组件-表格网格列
      dynamicTableCols:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableCols.length >
          0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableCols
          : [
            {
              prop: 'proportionSetId',
              label: 'ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'dlrCode',
              label: '门店编码'
            },
            {
              prop: 'dlrShortName',
              label: '门店名称'
            },
            {
              prop: 'planStatus',
              label: '计划状态'
            },
            {
              prop: 'makingMonth',
              label: '制定月份'
            },
            {
              prop: 'endDate',
              label: '制定截止日期'
            },
            {
              prop: 'ttCode',
              label: '22位码'
            },
            {
              prop: 'planMonth',
              label: '计划月份'
            },
            {
              prop: 'carSeriesCn',
              label: '车系名称'
            },
            {
              prop: 'carSeriesCode',
              label: '车系编码'
            },
            {
              prop: 'carSeriesCn',
              label: '车型名称'
            },
            {
              prop: 'carConfigCode',
              label: '车型编码'
            },
            {
              prop: 'optionalPagCode',
              label: '选装包编码'
            },
            {
              prop: 'optionalPagName',
              label: '选装包名称'
            },
            {
              prop: 'carColorCode',
              label: '车身颜色编码'
            },
            {
              prop: 'carColorName',
              label: '车身颜色名称'
            },
            {
              prop: 'carIncolorCode',
              label: '内饰颜色编码'
            },
            {
              prop: 'carIncolorName',
              label: '内饰颜色名称'
            },
            {
              prop: 'baseNum',
              label: '基数'
            },
            {
              prop: 'floatingCeiling',
              label: '浮动上限(%)'
            },
            {
              prop: 'floatingFloor',
              label: '浮动下限(%)'
            },
            {
              prop: 'updateControlId',
              label: '并发字段',
              hidden: true
            }
          ],
      // 单表格模版页面组件回调事件
      callbackObj: {
        // 当选择项发生变化时会触发该事件（多选时）
        onCheckSelectChange: (selection) => {

        },
        // 手动触发该事件（单选）
        onCheckSelect: (selection, row) => {
          this.handleSelectionChange(selection)
          // 清除所有选中
          this.$refs.multipleTable.$refs.multipleTable.clearSelection()
          if (selection.length === 0) return
          // 将当前点击项选中
          this.editRowData = row
          this.$refs.multipleTable.$refs.multipleTable.toggleRowSelection(row, true)
        },
        // 用户手动勾选全选
        onCheckSelectAll: () => {
          this.$refs['multipleTable'].$refs['multipleTable'].clearSelection()
          this.$message('请选择一项')
        }
      }
    }
  },
  mounted() {
    // 列表查询
    setTimeout(() => {
      this.queryTable(1)
    }, 0)
  },
  methods: {
    formulate() {
      this.dialogShow = true
      this.titleName = '比例设置'
      // this.editRowData = this.data
    },
    close(type) {
      this.dialogShow = false
      this.$refs['multipleTable'].$refs['multipleTable'].clearSelection()
      if (type === 2) {
        this.queryTable(1)
      }
    },
    // 当选择项发生变化时会触发该事件
    handleSelectionChange(selection) {
      const btns = this.dynamicButtons.filter(o => o.disabled !== undefined && o.parentFileds.indexOf('disabled:table') > -1)
      if (btns.length > 0) {
        const isCheck = selection && selection.length > 0 && selection[0].planStatus !== '已发布'
        btns[0].disabled = !isCheck
        const isTongbu = selection && selection.length > 0 && selection[0].baseNum && selection[0].floatingCeiling && selection[0].floatingFloor
        btns[1].disabled = !isTongbu
      }
    },
    car(queryData) {
      // // 先读取参数中的传输对象
      const returnObj = {
      // api配置
        apiConfig: this.apiConfig,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: queryData
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(returnObj, null, this.dynamicApiStyle)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD, this.dynamicApiStyle).then(response => {
        if (response.result === '0') {
          if (response.msg) {
            this.$message({ message: response.msg, type: 'warn', duration: 2000 })
          }
          return
        }
        this.$message({ message: response.msg })
        this.queryTable(1)
      }).catch(err => {
        console.info(err)
      })
    },
    // 同步
    synchro() {
      this.apiConfig = api.synStorePlanScaleSet
      var queryData = { proportionSetId: this.editRowData.proportionSetId }
      this.car(queryData)
      this.$refs['multipleTable'].$refs['multipleTable'].clearSelection()
    }
  }
}
</script>

